<?php
/**
 * Created by PhpStorm.
 * User: yzr
 * Date: 1/26/2021
 * Time: 16:36
 */
namespace app\common\model\job;


use app\common\model\base\A3Mall;

class Type extends A3Mall
{
    protected $name = "job_type";

    protected $type = [
        "id"=>"integer",
        "pid"=>"integer",
        "name"=>"string",
        "status"=>"integer",
    ];

    const STATUS_DISABLE = 0;   //禁用状态
    const STATUS_ENABLE = 1;    //启用状态


    /**
     * 获取分类
     * @param int $depth
     * @param null $status
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function fetchListByClassification($depth = 2,$status = null) {
        $where = [];
        if(!is_null($status))  {
            $where['status'] = $status;
        }
        $list = $this->fetchList($where);
        $result = [];
        foreach($list as $details) {
            if($details['pid'] == 0) {
                $result[$details['id']] = array_merge($details,[
                    'sublist'   => []
                ]);
            }
        }
        foreach($list as $details) {
            if($details['pid'] == 0 || !array_key_exists($details['pid'],$result)) {
                continue;
            }
            $result[$details['pid']]['sublist'][] = array_merge($details,[
                'sublist'   => []
            ]);
        }
        return $result;
    }


    /**
     * 查询结果集
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    private function fetchList($where) {
        $order = [
            'id'    => 'ASC',
        ];
        $list = $this->where($where)->order($order)->select()->toArray();
        return $list;
    }
}
